package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
* <p>
* 拼多多账单
* </p>
*
* @author huangrensen
* @since 2024-04-13
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PddBill extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;


    /**
    * 商户订单号
    */
    private String orderNo;


    /**
    * 发生时间
    */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;


    /**
    * 收入金额（+元）
    */
    private BigDecimal receiveAmount;


    /**
    * 支出金额（-元）
    */
    private BigDecimal payAmount;


    /**
    * 账务类型
    */
    private String billType;


    /**
    * 备注
    */
    private String remark;


    /**
    * 业务描述
    */
    private String businessRemark;


    private Long createBy;


    private Long updateBy;


    /**
    * 所属平台
    */
    private String platform;


    /**
    * 店铺ID
    */
    private Long storeId;
    private String storeName;

    @TableField(exist = false)
    private String tableName;

    public static boolean isDate(String field) {
        return "orderTime".contains(field);
    }

    public static boolean isDecimal(String field) {
        return ",receiveAmount,payAmount,".contains(","+field+",");
    }

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date1.equals(date2)) {
            return true;
        }
        return false;
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        PddBill pddBill = (PddBill) o;
        return Objects.equals(orderNo, pddBill.orderNo) && compareDate(orderTime, pddBill.orderTime) && compareBigDecimal(receiveAmount, pddBill.receiveAmount) && compareBigDecimal(payAmount, pddBill.payAmount) && Objects.equals(billType, pddBill.billType) && Objects.equals(remark, pddBill.remark) && Objects.equals(businessRemark, pddBill.businessRemark) && Objects.equals(createBy, pddBill.createBy) && Objects.equals(updateBy, pddBill.updateBy) && Objects.equals(platform, pddBill.platform) && Objects.equals(storeId, pddBill.storeId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), orderNo, orderTime, receiveAmount, payAmount, billType, remark, businessRemark, createBy, updateBy, platform, storeId);
    }

    // 截取长度超出范围的字段
    public void cutString() {
        if (this.remark != null && this.remark.length() > 2000) {
            this.remark = this.remark.substring(0, 2000);
        }
        if (this.businessRemark != null && this.businessRemark.length() > 2000) {
            this.businessRemark = this.businessRemark.substring(0, 2000);
        }
    }
}
